﻿Imports System.Collections
Imports System.Windows.Forms

Public Class clsColumnSorter
    Implements System.Collections.IComparer

    Private iColumnToSort As Integer
    Private pOrderOfSort As SortOrder
    Private pObjectCompare As CaseInsensitiveComparer

    Public Sub New()
        iColumnToSort = 0
        pOrderOfSort = SortOrder.None
        pObjectCompare = New CaseInsensitiveComparer
    End Sub

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        Dim iCompareResult As Integer = 0

        Dim pListViewX As ListViewItem = x
        Dim pListViewY As ListViewItem = y

        Dim pSortItemX As Object = pListViewX.SubItems(iColumnToSort).Tag
        Dim pSortItemY As Object = pListViewY.SubItems(iColumnToSort).Tag

        Select Case iColumnToSort
            Case 0 ' Datum
                iCompareResult = Date.Compare(pListViewX.SubItems(iColumnToSort).Text, pListViewY.SubItems(iColumnToSort).Text)

            Case 1 ' Preis
                    If CType(pSortItemX, Single) < CType(pSortItemY, Single) Then
                        iCompareResult = 1
                    ElseIf CType(pSortItemX, Single) > CType(pSortItemY, Single) Then
                        iCompareResult = -1
                    Else
                        iCompareResult = 0
                    End If

            Case 2 ' Liter
                    If CType(pSortItemX, Single) < CType(pSortItemY, Single) Then
                        iCompareResult = 1
                    ElseIf CType(pSortItemX, Single) > CType(pSortItemY, Single) Then
                        iCompareResult = -1
                    Else
                        iCompareResult = 0
                    End If

            Case 3 ' Preis/Liter
                    If CType(pSortItemX, Single) < CType(pSortItemY, Single) Then
                        iCompareResult = 1
                    ElseIf CType(pSortItemX, Single) > CType(pSortItemY, Single) Then
                        iCompareResult = -1
                    Else
                        iCompareResult = 0
                    End If

            Case 4 ' Tankstelle
                    iCompareResult = pObjectCompare.Compare(pListViewX.SubItems(iColumnToSort).Text, pListViewY.SubItems(iColumnToSort).Text)

            Case 5 'Kilometer
                    If CType(pSortItemX, Single) < CType(pSortItemY, Single) Then
                        iCompareResult = 1
                    ElseIf CType(pSortItemX, Single) > CType(pSortItemY, Single) Then
                        iCompareResult = -1
                    Else
                        iCompareResult = 0
                    End If

        End Select

        If pOrderOfSort = SortOrder.Descending Then
            Return -iCompareResult
        Else
            Return iCompareResult
        End If
    End Function

    Public Property SortColumn As Integer
        Get
            Return iColumnToSort
        End Get
        Set(ByVal value As Integer)
            iColumnToSort = value
        End Set
    End Property
    Public Property Order As SortOrder
        Get
            Return pOrderOfSort
        End Get
        Set(ByVal value As SortOrder)
            pOrderOfSort = value
        End Set
    End Property
End Class
